#include "delete_st.h"
#include "ui_delete_st.h"
#include<QSqlQuery>
#include"st_inf.h"
#include"ui_st_inf.h"
#include<QSqlQueryModel>

DELETE_st::DELETE_st(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::DELETE_st)
{
    ui->setupUi(this);
}

DELETE_st::~DELETE_st()
{
    delete ui;
}

//删除学生信息成功按钮
void DELETE_st::on_delete_2_clicked()
{
    QString id_name=ui->comboBox->currentText(),id="";
    //从id_name中提取id
    for(QChar s:id_name){
        if(s!=' ')id+=s;
        else break;
    }
    QSqlQuery query1,query2;
    query1.prepare("delete from st where id=:id");
    query1.bindValue(":id",id);

    query2.prepare("delete from gpa where id=:id");
    query2.bindValue(":id",id);
    if(query1.exec()&&query2.exec()){
        ui->tip->setText("删除成功！");
        st_inf*ST_inf=(st_inf*)parentWidget();

        //QSqlQueryModel结合QTableView显示数据
        QSqlQueryModel*model=new QSqlQueryModel;
        model->setQuery("select * from st");

        //设置表格头部信息
        model->setHeaderData(0,Qt::Horizontal,QObject::tr("学号"));
        model->setHeaderData(1,Qt::Horizontal,QObject::tr("姓名"));
        model->setHeaderData(2,Qt::Horizontal,QObject::tr("性别"));
        model->setHeaderData(3,Qt::Horizontal,QObject::tr("年龄"));

        ST_inf->ui->tableView->setModel(model);
    }

    else{
        ui->tip->setText("删除失败！");

    }

}

//返回查看学生信息界面
void DELETE_st::on_return_2_clicked()
{
    this->close();

}
